﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Bll.NoteBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.NoteModels;
using Sunny.UI;

namespace DevelopTool.Forms.NoteForms
{
    /// <summary>
    /// 笔记管理
    /// </summary>
    public partial class NoteManageForm : BaseUIPage
    {
        private ManageMenuBll manageMenull;

        private NoteBll noteBll;

        public NoteManageForm()
        {
            InitializeComponent();
        }

        private void NoteManageForm_Load(object sender, EventArgs e)
        {
            manageMenull = new ManageMenuBll();
            noteBll = new NoteBll();

            this.manageControl.BaseBll = noteBll;

            LoadManageControl();

            this.manageControl.BindContextMenuStripToNode(this.contextMenuStrip);
        }


        /// <summary>
        /// 加载管理页控件
        /// </summary>
        private void LoadManageControl()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据", () =>
            {
                manageMenus = noteBll.GetList().Cast<ManageMenu>().ToList();
            });

            this.manageControl.SetMenuData(manageMenus);
        }


        /// <summary>
        /// 菜单项添加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdded(object sender, MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;


            if (e.TreeNode.Name == "TopNode")
            {
                nav.SetNodeSymbol(e.TreeNode, 61641);
            }
            else if (e.TreeNode.Name == "Folder")
            {
                nav.SetNodeSymbol(e.TreeNode, 61564);
            }
            else if (e.TreeNode.Name == "Node")
            {
                nav.SetNodeSymbol(e.TreeNode, 361788);
            }
        }


        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="text"></param>
        private void ManageControl_MenuQuery(object sender, string text)
        {
            Dictionary<string, List<ManageMenu>> dic = new Dictionary<string, List<ManageMenu>>();

            //关联到的节点名
            var nameList = manageMenull.GetIdListByName<Note>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的笔记名", nameList);

            var timeList = manageMenull.GetIdListByTime<Note>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期", timeList);

            var textList = noteBll.GetListByText(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的笔记内容", textList);

            var fileList = noteBll.GetListByFileName(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的附件名", fileList);

            this.manageControl.SetMenuQueryData(dic);
        }


        /// <summary>
        /// 菜单节点双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDoubleClick(object sender, MenuEventArgs e)
        {
            if (e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                Note note = noteBll.GetById(e.ManageMenu.Id);

                this.manageControl.ShowPageForm(new NoteForm(note), note.NodeName, true, note.NodeName + note.Id);
            }
        }

        /// <summary>
        /// 菜单节点拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDrag(object sender, MenuEventArgs e)
        {
            Note note = manageMenull.GetExample<Note>(e.ManageMenu);

            manageMenull.SimpleSave(note);
        }





        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDelete(object sender, MenuEventArgs e)
        {
            Note note = manageMenull.GetExample<Note>(e.ManageMenu);

            if (noteBll.Delete(note.Id))
            {
                this.ShowSuccessNotifier($"【{note.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空目录");
            }
        }


        /// <summary>
        /// 重新加载菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuNeedReLoad(object sender, EventArgs e)
        {
            LoadManageControl();
        }




        /// <summary>
        /// 菜单节点重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuRename(object sender, MenuEventArgs e)
        {
            Note note = manageMenull.GetExample<Note>(e.ManageMenu);

            manageMenull.SimpleSave(note);
        }


        /// <summary>
        /// 新建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdd(object sender, MenuEventArgs e)
        {
            Note note = manageMenull.GetExample<Note>(e.ManageMenu);

            note = manageMenull.SimpleSave(note);

            e.ManageMenu = note;

            this.ShowSuccessNotifier("新建成功");
        }



        private void 推送到飞书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Note note = noteBll.GetById(this.manageControl.SelectedManageMenu.Id);

            if (note != null && !string.IsNullOrEmpty(note.Text))
            {
                Tuple<bool, string> sendResult = new Tuple<bool, string>(false, "推送失败");
                LoadingHelper.ShowLoading("正在推送内容", () =>
                {
                    sendResult = FeiShuMsgHelper.SendCardMsg(note.Text, $"笔记-{note.NodeName}", contentType: note.TextType);
                });

                if (sendResult.Item1)
                {
                    this.ShowSuccessNotifier("推送内容成功");
                }
                else
                {
                    this.ShowWarningNotifier(sendResult.Item2);
                }
            }

        }
    }
}
